﻿using System.Configuration;
using System.Diagnostics;

namespace ReadyApps.Common
{
    class ReadyAppsConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("ckeditorBasePath")]
        public string CKEditorBasePath
        {
            get { return (string)this["ckeditorBasePath"]; }
        }

        [ConfigurationProperty("traceSettings")]
        public TraceSettings TraceSettings
        {
            get { return (TraceSettings)this["traceSettings"]; }
        }

        [ConfigurationProperty("wikiDataSettings", IsRequired = true)]
        public WikiDataSettingsElement WikiDataSettings
        {
            get { return (WikiDataSettingsElement)this["wikiDataSettings"]; }
        }

        [ConfigurationProperty("wikiMappings", IsDefaultCollection = false)]
        public WikiMappingsCollectionElement WikiMappings
        {
            get { return (WikiMappingsCollectionElement)this["wikiMappings"]; }
        }
    }

    class WikiDataSettingsElement : ConfigurationElement
    {
        [ConfigurationProperty("connectionString", IsRequired = true)]
        public string ConnectionStringName
        {
            get { return (string)this["connectionString"]; }
        }

        [ConfigurationProperty("typeName", IsRequired = true)]
        public string TypeName
        {
            get { return (string)this["typeName"]; }
        }

        [ConfigurationProperty("assembly")]
        public string Assembly
        {
            get { return (string)this["assembly"]; }
        }
    }

    class TraceSettings : ConfigurationElement
    {
        [ConfigurationProperty("enabledComponents")]
        public TraceComponent EnabledComponents
        {
            get { return (TraceComponent)this["enabledComponents"]; }
        }

        [ConfigurationProperty("level")]
        public TraceLevel Level
        {
            get { return (TraceLevel)this["level"]; }
        }
    }

    class WikiMappingsElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
        }

        [ConfigurationProperty("urlPrefix")]
        public string UrlPrefix
        {
            get { return (string)this["urlPrefix"]; }
        }

        [ConfigurationProperty("mapTo")]
        public string MapTo
        {
            get { return (string)this["mapTo"]; }
        }
    }

    class WikiMappingsCollectionElement : ConfigurationElementCollection
    {
        #region Properties

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        #endregion

        #region Methods

        protected override ConfigurationElement CreateNewElement()
        {
            return new WikiMappingsElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((WikiMappingsElement)element).Name;
        }

        #endregion
    }
}
